﻿
var _table = null;

layui.use(['form', 'layer'], function () {
    var form = layui.form,
        layer = layui.layer;

    $(function () {
        _table = $('#table');
        loadList();
    });
});

//查询
function search() {
    _table.bootstrapTable('destroy');
    loadList();
}

//初始化表格
function loadList() {
    app.list('Organization.ashx', {
        ActionType: 'Organization',
        OrganizationName: $('#tbKey').val()
    }, function (data) {
        _table.bootstrapTable({
            data: data,
            idField: 'GUID',
            singleSelect: true,
            dataType: 'jsonp',

            // bootstrap-table-treegrid.js 插件配置 -- start
            //在哪一列展开树形

            treeShowField: 'OrganizationName',
            //指定父id列
            parentIdField: 'ParentId',
            onResetView: function (data) {
                //console.log('load');
                _table.treegrid({
                    //initialState: 'collapsed', // 所有节点都折叠
                    initialState: 'expanded',// 所有节点都展开，默认展开
                    treeColumn: 1,
                    // expanderExpandedClass: 'glyphicon glyphicon-minus',  //图标样式
                    // expanderCollapsedClass: 'glyphicon glyphicon-plus',
                    onChange: function () {
                        _table.bootstrapTable('resetWidth');
                    }
                });

                //只展开树形的第一级节点
                //$table.treegrid('getRootNodes').treegrid('expand');
            }
            // bootstrap-table-treetreegrid.js 插件配置 -- end
        });
    });
}

//格式化操作按钮
function formatterOperationMenu(value, row, index) {
    return app.getOperationMenu('GUID', row);
}

//新增
function add() {
    var pid = '';
    var selRows = _table.bootstrapTable("getSelections");
    if (selRows.length > 1) {
        layer.msg("请选择一行");
        return;
    }
    if (selRows.length == 1) {
        pid = selRows[0].GUID;
    }

    layer.open({
        title: "组织机构",
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "OrganizationSet.aspx?ParentId=" + pid
    });
}

//编辑
function edit(id) {
    layer.open({
        title: "组织机构",
        type: 2,
        area: ["100%", "100%"],
        scrollbar: false,
        content: "OrganizationSet.aspx?GUID=" + id
    });
}

//删除
function del(id) {
    layer.confirm('确定删除?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        app.del('Organization.ashx', {
            ActionType: 'Organization',
            GUID: id
        }, function (data) {
            layer.msg(data.msg);
            if (data.success) {
                search();
            }
        });
        layer.close(index);
    });
}
